package com.atguigu.tingshu.account.mq;

import com.atguigu.tingshu.account.service.RechargeInfoService;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import com.atguigu.tingshu.account.service.UserAccountService;

@Component
@Slf4j
public class AccountReceiver {

    @Autowired
    private UserAccountService userAccountService ;
    @Autowired
    private RechargeInfoService rechargeInfoService;

    @KafkaListener(topics = KafkaConstant.QUEUE_USER_REGISTER)
    public void addUserAccount(ConsumerRecord<String , String> record) {

        String value = record.value();      // 获取到消息的内容
        if(!StringUtils.isEmpty(value)) {
            userAccountService.addUserAccount(value);
            log.info("用户账户信息初始化成功了...userId: {}" , value );
        }

    }

    @KafkaListener(topics = KafkaConstant.QUEUE_ACCOUNT_MINUS)
    public void minusAccount(ConsumerRecord<String , String> record) {
        String value = record.value();
        if(!StringUtils.isEmpty(value)) {
            userAccountService.minusAccount(value) ;
            log.info("账户的余额扣减成功了...orderNo: {}" , value);
        }
    }

    @KafkaListener(topics = KafkaConstant.QUEUE_ACCOUNT_UNLOCK)
    public void unLockAccount(ConsumerRecord<String , String> record) {
        String value = record.value();
        if(!StringUtils.isEmpty(value)) {
            userAccountService.unLockAccount(value) ;
            log.info("账户锁定金额解锁成功...orderNo: {}" , value);
        }
    }

    @KafkaListener(topics = KafkaConstant.QUEUE_ORDER_PAY_SUCCESS)
    public void rechargePaySuccess(ConsumerRecord<String,String> record){
        String orderNo = record.value();
        if (!StringUtils.isEmpty(orderNo)){
            rechargeInfoService.rechargePaySuccess(orderNo);
            log.info("充值成功通知: {}", orderNo);
        }

    }
}